﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software component that provide 
// support for localization. This library enable you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace System
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;

    //LocalizationLibrary References
    using SLS.ExClassLib.Loc;

    /// <summary>
    /// Defines <see cref="String"/>'s extensions.
    /// </summary>
    public static class StringExtension
    {
        /// <summary>
        /// Get the localized value for the resource identifier defined in the <see cref="String"/>.
        /// The value is localized in the current language.
        /// </summary>
        /// <param name="instance">Instance.</param>
        /// <returns>Value localized in the current language.</returns>
        public static Object Localize(this String instance)
        {
            return StringExtension.Localize(instance, true);
        }

        /// <summary>
        /// Get the localized value for the resource identifier defined in the <see cref="String"/>.
        /// The value is localized in the current language.
        /// </summary>
        /// <param name="instance">Instance.</param>
        /// <param name="waitIfNotLocalized">If true wait for localization, otherwise returns null.</param>
        /// <returns>Value localized in the current language.</returns>
        public static Object Localize(this String instance, bool waitIfNotLocalized)
        {
            return StringExtension.Localize(instance, LocalizationManager.CurrentLanguage.Code, waitIfNotLocalized);
        }

        /// <summary>
        /// Get the localized value for the resource identifier defined in the <see cref="String"/>.
        /// The value is localized in the input language.
        /// </summary>
        /// <param name="instance">Instance.</param>
        /// <param name="language">Traget language.</param>
        /// <returns>Value localized in the input language.</returns>
        /// <exception cref="ArgumentException">
        /// Thrown whether the input language is an invalid language.
        /// </exception>
        public static Object Localize(this String instance, String language)
        {
            return StringExtension.Localize(instance, language, true);
        }

        /// <summary>
        /// Get the localized value for the resource identifier defined in the <see cref="String"/>.
        /// The value is localized in the input language.
        /// </summary>
        /// <param name="instance">Instance.</param>
        /// <param name="language">Traget language.</param>
        /// <param name="waitIfNotLocalized">If true wait for localization, otherwise returns null.</param>
        /// <returns>Value localized in the input language.</returns>
        /// <exception cref="ArgumentException">
        /// Thrown whether the input language is an invalid language.
        /// </exception>
        public static Object Localize(this String instance, String language, bool waitIfNotLocalized)
        {
            if ((language == null) || (!Language.Validate(language)))
            {
                throw new ArgumentException("Invalid language.");
            }
            double maxMillisecondForLoc = LocalizationManager.MaxWaitingMillisecondForLocalization;
            DateTime startTime = DateTime.Now;
            Object retVal = null;
            do
            {
                if ((DateTime.Now - startTime).TotalMilliseconds > maxMillisecondForLoc)
                {
                    break;
                }
                DynamicResourceDescription dynResDesc = LocalizationManager.GetResourceDescription(instance);
                if (dynResDesc != null)
                {
                    dynResDesc.EnforceLocalization((Language)language);
                    retVal = dynResDesc[(Language)language];
                    if ((waitIfNotLocalized) && (retVal == null))
                    {
                       
                    }
                }
                else 
                {
                    break;
                }
            } while ((waitIfNotLocalized) && (retVal == null));
            return retVal;
        }
    }
}